package controllers

import (
	"github.com/astaxie/beego"
	m"xmy/models/admin"
	"fmt"
	"strconv"
	"reflect"
)

type HomeController struct {
	beego.Controller
}

//返回值 封装为 JSON 格式
func (this *HomeController) Rsp(status bool, str string) {
	this.Data["json"] = &map[string]interface{}{"status": status, "info": str}
	this.ServeJSON()
}

func (this *HomeController)  HomePage() {
	page, _ := this.GetInt64("page")
	page_size, _ := this.GetInt64("rows")
	sort := this.GetString("sort")
	order := this.GetString("order")
	fmt.Println("order:", order)
	if len(order) > 0 {
		if order == "desc" {
			sort = "-" + sort
		}
	} else {
		sort = "Id"
	}
	nodes, count := m.GetGrouplist(page, page_size, sort)
	this.Data["json"] = &map[string]interface{}{"total": count, "rows": &nodes}
	fmt.Println(this.Data["json"])
	this.ServeJSON()
	return
}

func (c *HomeController)  NewsManage() {
	fmt.Println("进来--新闻管理--控制器啦！")
	c.TplName = "home/newsManage.html"
}
//新闻列表
func (this *HomeController)  NewsPage() {
	fmt.Println("进来--新闻列表--控制器啦！")
	neswTitle := this.GetString("neswTitle")
	newsType := this.GetString("newsType")
	firstTime := this.GetString("firstTime")
	lastTime := this.GetString("lastTime")
	fmt.Println("firstTime...................",firstTime)
	fmt.Println("lastTime...................",lastTime)

	nodes := m.GetNewslist(neswTitle,newsType,firstTime,lastTime)
	this.Data["json"] = &map[string]interface{}{"rows": &nodes}
	fmt.Println(this.Data["json"])
	this.ServeJSON()
	return
}

////新闻列表
//func (this *HomeController)  NewsPage() {
//	fmt.Println("进来--新闻列表--控制器啦！")
//	page, _ := this.GetInt64("page")
//	fmt.Println("page:", page)
//	page_size, _ := this.GetInt64("rows")
//	fmt.Println("page_size:", page_size)
//	sort := this.GetString("sort")
//	fmt.Println("sort:", sort)
//	order := this.GetString("order")
//	fmt.Println("order:", order)
//	if len(order) > 0 {
//		if order == "desc" {
//			sort = "-" + sort
//		}
//	} else {
//		sort = "Id"
//	}
//	nodes, count := m.GetNewslist(page, page_size, sort)
//	this.Data["json"] = &map[string]interface{}{"total": count, "rows": &nodes}
//	fmt.Println(this.Data["json"])
//	this.ServeJSON()
//	return
//}




//添加新闻
func (c *HomeController)  AddNews() {
	fmt.Println("进来--添加新闻--控制器啦！")

	Title := c.GetString("Title")
	newsType := c.GetString("Newstype")
	Author := c.GetString("Author")
	Newscontent := c.GetString("Newscontent")
	fmt.Println("Title...........",Title)
	fmt.Println("newsType...........",newsType)
	fmt.Println("Author...........",Author)
	fmt.Println("Newscontent...........",Newscontent)


	id:= m.AddNews(Title,newsType,Author,Newscontent)
	fmt.Println("-----id:", id)
	if  id > 0 {
		c.Rsp(true, "Success")
		return
	} else {
		c.Rsp(false, "error")
		return
	}
}
////添加新闻
//func (c *HomeController)  AddNews() {
//	fmt.Println("进来--添加新闻--控制器啦！")
//	g := m.News{}
//	if err := c.ParseForm(&g); err != nil {
//		//handle error
//		c.Rsp(false, err.Error())
//		return
//	}
//	id, err := m.AddNews(&g)
//	fmt.Println("-----id:", id)
//	if err == nil && id > 0 {
//		c.Rsp(true, "Success")
//		return
//	} else {
//		c.Rsp(false, err.Error())
//		return
//	}
//}
//加入回收站
func (c *HomeController)  RecycleNew() {
	fmt.Println("进来--加入回收站--控制器啦！")
	Id := c.GetString("Id")
	News_id, err := strconv.ParseInt(Id, 10, 64)
	//打印参数类型
	fmt.Println("type:", reflect.TypeOf(News_id))
	id, err := m.Recycle_News_One(News_id)
	if err == nil && id > 0 {
		fmt.Println("wo lai l l aa a  a ")
		c.Rsp(true, "1")
		return
	} else {
		c.Rsp(true, "0")
		return
	}
}
//回收站管理
func (c *HomeController)  NewsManageRecycle() {
	fmt.Println("进来--新闻回收站管理--控制器啦！")
	c.TplName = "home/newsManageRecycle.html"
}

//回收站管理列表
func (this *HomeController)  NewsManageRecyclePage() {
	fmt.Println("进来--新闻回收站列表--控制器啦！")
	page, _ := this.GetInt64("page")
	fmt.Println("page:", page)
	page_size, _ := this.GetInt64("rows")
	fmt.Println("page_size:", page_size)
	sort := this.GetString("sort")
	fmt.Println("sort:", sort)
	order := this.GetString("order")
	fmt.Println("order:", order)
	if len(order) > 0 {
		if order == "desc" {
			sort = "-" + sort
		}
	} else {
		sort = "Id"
	}
	nodes, count := m.GetNewsRecyclelist(page, page_size, sort)
	this.Data["json"] = &map[string]interface{}{"total": count, "rows": &nodes}
	fmt.Println(this.Data["json"])
	this.ServeJSON()
	return

}

//从回收站还原新闻
func (c *HomeController)  ReductionNew() {
	fmt.Println("进来--还原新闻--控制器啦！")
	Id := c.GetString("Id")
	News_id, err := strconv.ParseInt(Id, 10, 64)
	//打印参数类型
	fmt.Println("type:", reflect.TypeOf(News_id))
	id, err := m.Reduction_News_One(News_id)
	if err == nil && id > 0 {
		c.Rsp(true, "1")
		return
	} else {
		c.Rsp(true, "0")
		return
	}
}

//从回收站删除新闻
func (c *HomeController)  DeleteNew() {
	fmt.Println("进来--删除新闻--控制器啦！")
	Id := c.GetString("Id")
	News_id, err := strconv.ParseInt(Id, 10, 64)
	//打印参数类型
	fmt.Println("type:", reflect.TypeOf(News_id))
	id, err := m.Delete_News_One(News_id)
	if err == nil && id > 0 {
		c.Rsp(true, "1")
		return
	} else {
		c.Rsp(true, "0")
		return
	}
}
